import { watch } from 'vue';
import {
  FileContainer,
  usePlatformUploadStore,
} from 'src/stores/platform-upload';
import { usePlatformReUploadStore } from 'stores/platform-reupload';
import {usePlatformFeatureSetUploadStore} from "stores/platform-featureset-upload";
import {usePlatformReUploadFeatureSetStore} from "stores/platform-featureset-reupload";

const platformUploadStore = usePlatformUploadStore();
const platformReUploadStore = usePlatformReUploadStore();
const platformFeatureSetUploadStore = usePlatformFeatureSetUploadStore()
const platformFeatureReUploadStore = usePlatformReUploadFeatureSetStore()

const processFeatureSet = (process_id: string, key: any) => {
  const fileItem =
    platformFeatureSetUploadStore.platformFeatureSetFileContainer[process_id][
      key as keyof FileContainer
      ];
  platformFeatureSetUploadStore.platformUploadFileList[process_id]?.map((vueFileItem) => {
    const filename = vueFileItem?.name;
    const fileSize = vueFileItem?.raw?.size as number;

    let curLoadSize = 0;
    if (!fileItem || !fileItem[filename] || !fileItem[filename].length) return;

    fileItem[filename].forEach((fileChunk) => {
      curLoadSize += fileChunk.percentage * fileChunk.size;
    });

    vueFileItem.percentage = parseInt((curLoadSize / fileSize).toFixed(2));
    platformFeatureSetUploadStore.platformUploadList.forEach((platformUploadItem) => {
      if (
        platformUploadItem.feature_set_id == Number(vueFileItem.feature_set_id) &&
        platformUploadItem.row_name == filename
      ) {
        platformUploadItem.progress = vueFileItem.percentage!
        if (platformUploadItem.progress == 100) {
          platformUploadItem.status = 'success';
        }
      }
    });
  });
};

const process = (process_id: string, key: any) => {
  const fileItem =
    platformUploadStore.platformDataSetFileContainer[process_id][
      key as keyof FileContainer
    ];
  platformUploadStore.platformUploadFileList[process_id]?.map((vueFileItem) => {
    const filename = vueFileItem?.name;
    const fileSize = vueFileItem?.raw?.size as number;

    let curLoadSize = 0;
    if (!fileItem || !fileItem[filename] || !fileItem[filename].length) return;

    fileItem[filename].forEach((fileChunk) => {
      curLoadSize += fileChunk.percentage * fileChunk.size;
    });

    vueFileItem.percentage = parseInt((curLoadSize / fileSize).toFixed(2));
    platformUploadStore.platformUploadList.forEach((platformUploadItem) => {
      if (
        platformUploadItem.dataset_id == vueFileItem.dataset_id &&
        platformUploadItem.version_id == vueFileItem.version_id &&
        platformUploadItem.row_name == filename
      ) {
        platformUploadItem.progress = vueFileItem.percentage!;

        if (platformUploadItem.progress == 100) {
          platformUploadItem.status = 'success';
        }
      }
    });
  });
};
export const useWatchProgress = (key: keyof FileContainer, fileContainerType: 'dataset' | 'featureSet') => {
  watch(
    () => fileContainerType === 'dataset' ?  platformUploadStore.platformDataSetFileContainer : platformFeatureSetUploadStore.platformFeatureSetFileContainer,
    () => {
      const store = fileContainerType == 'dataset' ? platformUploadStore : platformFeatureSetUploadStore

      platformReUploadStore.reUploadProcessId.forEach((id) =>
        process(String(id), key)
      );

      platformFeatureReUploadStore.reUploadProcessId.forEach((id)  => {
        processFeatureSet(String(id), key)
      })

      store.platformCurrentProcessId.forEach((id) => {
        if (fileContainerType == 'dataset') {
          process(String(id), key)

        } else {
          processFeatureSet(String(id), key)
        }
      })
    },
    { deep: true }
  );
};
